package com.aipanda.entity;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * @param 	name 				产品种类名称
 * @param 	seoTitle 			页面标题
 * @param 	seoKeywords 		页面关键词
 * @param 	seoDescription 		页面描述
 * @param 	treePath 			分类的路径
 * @param 	grade 				级别
 * @param 	parent 				父类 [多对一关系]
 * @param 	children 			子类  [一对多关系]
 * @param 	products 			产品  [一对多关系]
 * @param 	brands 				商标  [多对多关系]
 * @param 	parameterGroups 	商品参数 [一对多关系]
 * @param 	attributes    		商品属性 [一对多关系]
 * @param 	promotions 			促销 [多对多关系]
 * @author wanyuxiang
 * 
 */
@Entity
@Table(name = "aipanda_product_category")
public class ProductCategory extends OrderEntity {
	private static final long serialVersionUID = 5095521437302782717L;
	public static final String TREE_PATH_SEPARATOR = ",";
	private String name;
	private String seoTitle;
	private String seoKeywords;
	private String seoDescription;
	private String treePath;
	private Integer grade;
	private ProductCategory parent;
	private Set<ProductCategory> children = new HashSet<ProductCategory>();
	private Set<Product> products = new HashSet<Product>();
	private Set<Brand> brands = new HashSet<Brand>();
	private Set<ParameterGroup> parameterGroups = new HashSet<ParameterGroup>();
	private Set<Attribute> attributes = new HashSet<Attribute>();
	private Set<Promotion> promotions = new HashSet<Promotion>();

	@NotEmpty
	@Length(max = 255)
	@Column(nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Length(max = 255)
	@Column(name="seo_title")
	public String getSeoTitle() {
		return this.seoTitle;
	}

	public void setSeoTitle(String seoTitle) {
		this.seoTitle = seoTitle;
	}

	@Length(max = 255)
	@Column(name="seo_keywords")
	public String getSeoKeywords() {
		return this.seoKeywords;
	}

	public void setSeoKeywords(String seoKeywords) {
		this.seoKeywords = seoKeywords;
	}

	@Length(max = 255)
	@Column(name="seo_description")
	public String getSeoDescription() {
		return this.seoDescription;
	}

	public void setSeoDescription(String seoDescription) {
		this.seoDescription = seoDescription;
	}

	@Column(name="tree_path",nullable = false)
	public String getTreePath() {
		return this.treePath;
	}

	public void setTreePath(String treePath) {
		this.treePath = treePath;
	}

	@Column(nullable = false)
	public Integer getGrade() {
		return this.grade;
	}

	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="parent",nullable = false)
	public ProductCategory getParent() {
		return this.parent;
	}

	public void setParent(ProductCategory parent) {
		this.parent = parent;
	}

	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
	@OrderBy("order asc")
	public Set<ProductCategory> getChildren() {
		return this.children;
	}

	public void setChildren(Set<ProductCategory> children) {
		this.children = children;
	}

	@OneToMany(mappedBy = "productCategory", fetch = FetchType.LAZY)
	public Set<Product> getProducts() {
		return this.products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "aipanda_product_category_brand",joinColumns ={@JoinColumn(name="product_categories",nullable=false)},inverseJoinColumns = {@JoinColumn(name="brands",nullable=false)})
	@OrderBy("order asc")
	public Set<Brand> getBrands() {
		return this.brands;
	}

	public void setBrands(Set<Brand> brands) {
		this.brands = brands;
	}

	@OneToMany(mappedBy = "productCategory", fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	@OrderBy("order asc")
	public Set<ParameterGroup> getParameterGroups() {
		return this.parameterGroups;
	}

	public void setParameterGroups(Set<ParameterGroup> parameterGroups) {
		this.parameterGroups = parameterGroups;
	}

	@OneToMany(mappedBy = "productCategory", fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	@OrderBy("order asc")
	public Set<Attribute> getAttributes() {
		return this.attributes;
	}

	public void setAttributes(Set<Attribute> attributes) {
		this.attributes = attributes;
	}

	@ManyToMany(mappedBy = "productCategories", fetch = FetchType.LAZY)
	public Set<Promotion> getPromotions() {
		return this.promotions;
	}

	public void setPromotions(Set<Promotion> promotions) {
		this.promotions = promotions;
	}

	@Transient
	public List<Long> getTreePaths() {
		ArrayList<Long> list = new ArrayList<Long>();
		String[] treeDeep = StringUtils.split(getTreePath(), ",");
		if (treeDeep != null){
			for (String deep : treeDeep){
				list.add(Long.valueOf(deep));
			}
		}
		return list;
	}

	@Transient
	public String getPath() {
		if (getId() != null){
			return "/product/list/" + getId() + ".jhtml";
		}
		return null;
	}

	public ProductCategory preRemove(ProductCategory productCategory) {
		Set<Promotion> currentPromotions = productCategory.getPromotions();
		if (currentPromotions != null) {
			Iterator<Promotion> itPromotions = currentPromotions.iterator();
			while (itPromotions.hasNext()) {
				Promotion promotion = itPromotions.next();
				promotion.getProductCategories().remove(this);
			}
		}
		return productCategory;
	}
}

